﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Globalization;

namespace LastfmNet.Models
{
    /// <summary>
    /// http://www.w3.org/2003/01/geo/wgs84_pos#
    /// </summary>
    public class GeoPoint : LastfmModelBase
    {
        public GeoPoint()
        {
            this.Latitude = 0;
            this.Longitude = 0;
        }

        public GeoPoint(XmlNode geoPointNode)
        : this()
        {
            if (geoPointNode == null) return;
            this.InitializeFromXmlNode(geoPointNode);
        }

        public override void InitializeFromXmlNode(XmlNode node)
        {
            if (node == null) return;
            this.Latitude = node.GetChildValue("geo:lat").ToFloat();
            this.Longitude = node.GetChildValue("geo:long").ToFloat();
        }

        public override string ToString()
        {
            return string.Format(CultureInfo.InvariantCulture, "{{Latitude = {0}, Longitude = {1}}}", Latitude, Longitude);
        }

        public override bool Equals(object obj)
        {
            if (obj == null || GetType() != obj.GetType())
            {
                return false;
            }
            GeoPoint compareWith = obj as GeoPoint;
            return (this.Latitude == compareWith.Latitude
                    && this.Longitude == compareWith.Longitude);
        }

        public override int GetHashCode()
        {
            return this.Latitude.GetHashCode() ^ this.Longitude.GetHashCode();
        }

        public float Latitude { get; set; }

        public float Longitude { get; set; }
    }
}
